package com.smart.modules.genealogy.controller;


import com.smart.modules.genealogy.vo.PersonEventVo;
import com.smart.modules.genealogy.vo.PersonRelationshipVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.modules.genealogy.domain.PersonEvent;
import com.smart.modules.genealogy.service.IPersonEventService;
import com.smart.system.common.result.Result;
import com.smart.system.common.result.PageResult;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 人员事件Controller
 */
@RestController
@RequestMapping("/genealogy/event")
public class PersonEventController
{
    @Autowired
    private IPersonEventService personEventService;

    /**
     * 查询人员事件列表
     */
    @GetMapping("/list")
    public PageResult list(PersonEvent personEvent)
    {
        IPage<PersonEvent> pageModel = new Page<PersonEvent>(personEvent.getPageNum(),personEvent.getPageSize());
        IPage<PersonEvent> page = personEventService.selectPersonEventList(pageModel,personEvent);
        return PageResult.success(page);
    }
    /**
     * 查询人员详细信息
     */
    @GetMapping("/getPersonDetail/{personId}")
    public Result getPersonDetail(@PathVariable Long personId)
    {
        Map<String,Object> map = new HashMap<>();
        List<PersonEventVo> personEvent = personEventService.getPersonDetail(personId);
        List<PersonRelationshipVo> personRelationship = personEventService.getPersonRelationship(personId);
        map.put("personEvent",personEvent);
        map.put("personRelationship",personRelationship);
        return Result.success(map);
    }


    /**
     * 获取人员事件详细信息
     */
    //@PreAuthorize("@ss.hasPermi('genealogy:event:query')")
    @GetMapping(value = "/{personId}")
    public Result getInfo(@PathVariable("personId") Long personId)
    {
        PersonEvent personEvent = personEventService.selectPersonEventByPersonId(personId);
        String eventAttachment = personEvent.getEventAttachment();
        if (eventAttachment != null){
            String[] list = eventAttachment.split(",");
            personEvent.setEventAttachmentList(Arrays.stream(list).toList());
        }

        return Result.success(personEvent);
    }

    /**
     * 新增人员事件
     */
    //@PreAuthorize("@ss.hasPermi('genealogy:event:add')")
    @PostMapping
    public Result add(@RequestBody PersonEvent personEvent)
    {
        return Result.success(personEventService.insertPersonEvent(personEvent));
    }

    /**
     * 修改人员事件
     */
    //@PreAuthorize("@ss.hasPermi('genealogy:event:edit')")
    @PutMapping
    public Result edit(@RequestBody PersonEvent personEvent)
    {
        return Result.success(personEventService.updatePersonEvent(personEvent));
    }

    /**
     * 删除人员事件
     */
    //@PreAuthorize("@ss.hasPermi('genealogy:event:remove')")
    @DeleteMapping("/{personIds}")
    public Result remove(@PathVariable Long[] personIds)
    {
        return Result.success(personEventService.deletePersonEventByPersonIds(personIds));
    }
}
